﻿using System;
using System.Buffers;
using System.Linq;
using System.Text;

using Microsoft.Extensions.DependencyInjection;

using Xc.DataAccess.Core.Extensions;

namespace Xc.DataAccess.PcpChannels
{
    /// <summary>
    /// 上报升级结果
    /// </summary>
    public class DeviceUploadUpdateResultRequest : RespondFrame
    {
        public DeviceUploadUpdateResultRequest(byte[] dataarea) : base(dataarea)
        {

        }

        protected override MessageCode? ValidateMessageCode => MessageCode.UploadUpdateResult;
        public UploadUpdateResult Status { get; set; }
        public string CurrentVersion { get; set; }
        public enum UploadUpdateResult
        {
            /// <summary>
            /// 允许升级
            /// </summary>
            Success = 0x00,
            /// <summary>
            /// 设备使用中
            /// </summary>
            DeviceOnUse = 0x01,
            /// <summary>
            /// 电量不足
            /// </summary>
            VoltageLow = 0x04,
            /// <summary>
            /// 剩余空间不足
            /// </summary>
            SpaceInsufficient = 0x05,
            /// <summary>
            /// 内存不足
            /// </summary>
            OutOfMemory = 0x09,
            /// <summary>
            /// 安装包升级失败
            /// </summary>
            UpgradeFailed = 0x0A,
            /// <summary>
            /// 内部异常
            /// </summary>
            InnerException = 0x7F
        }

        protected override bool SetDataArea(ref System.Buffers.SequenceReader<byte> dataArea)
        {
            if (dataArea.TryRead(out var c))
            {
                Status = (UploadUpdateResult)c;
                if (dataArea.TryRead(16,out var e))
                {
                    CurrentVersion = Encoding.ASCII.GetString(e.ToArray().Where(p => p != 0).ToArray());
                    return true;
                }
            }
            return false;

        }
    }
}
